<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/common.css">
    <style>
        .start,
        .stop {
            width: 1rem;
            height: .3rem;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="option">
            <button class="start">start</button>
            <button class="stop">stop</button>
        </div>
        <div class="txt">
        </div>
    </div>
</body>
<script>
    let s = 10
    let id
    let txt = document.querySelector('.txt')
    let start = document.querySelector('.start')
    //开始计时
    //addEventListener监听事件
    start.addEventListener('click', function () {
        if (id) {
            //清空定时器
            clearInterval(id)
        }
        //setInterval定时器
        id = setInterval(function () {
            txt.innerHTML = s
            s--
            if (s === 0) {
                txt.innerHTML = s
                clearInterval(id)
                //禁用按钮
                start.disabled = true
                return
            }
        }, 1000)
    }, false)
    //停止计时
    let stop = document.querySelector('.stop')
    stop.addEventListener('click', function () {
        if (id) {
            clearInterval(id)
        }
    }, false)
</script>

</html>